<?php

namespace App\Http\Controllers;

use App\Services\RequestService;
use Dingo\Api\Routing\Helpers;
use Aitifen\Library\Common\MicroService\Client;
use Illuminate\Http\Request;
use Aitifen\Library\Xxtea\Xxtea;
use Illuminate\Support\Facades\Config;
use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    protected $appKey;
    protected $appSecret;
    public $token;
    /**
     *  加载appkey,appsecret,accesstoken
     */
    public function __construct(Request $request)
    {
        $token   = isset($_COOKIE['USER-ACCESS-TOKEN']) ? $_COOKIE['USER-ACCESS-TOKEN'] : '';
        if (empty($token)) {
            $token   = $request->header('USER-ACCESS-TOKEN');
        }
        $this->token = $token;

        //app_key|app_secret设置
        $appData = isset($_COOKIE['app_data']) ? $_COOKIE['app_data'] : '';
        if (isset($appData) && !empty($appData)) {
            $appData = Xxtea::decrypt($appData, Config::get('common.cookie_encrypt_key'), true);
            $this->appKey    = $appData['app_key'];
            $this->appSecret = $appData['app_secret'];
        }

        // 验证权限
        $bool = $this->checkAccess();
        if ($bool == false) {
            $returnData = [
                    'code' => 1001,
                    'message' => '用户AccessToken不存在或已过期!',
                    'data' => [],
                ];
                echo json_encode($returnData,JSON_UNESCAPED_UNICODE);exit;
        }

        if (empty($appData) && ($request->is('api/backend/users/getLoginStatus') == false)) {
            header("Location:"."/api/backend/users/getLoginStatus");
        }
    }

    /**
     * 获取微服务对象
     * @param $service  服务名称
     * @param $module   调用模块
     * @return object
     */
    protected function getClient($service, $module)
    {
        try {
            $client = Client::getClient($service, $module);
            $client->setHeaders($this->appKey, $this->appSecret, $this->token);
            return $client;
        } catch (\Exception $e) {
            echo $e->getMessage();die;
        }
    }

    /**
     *  返回json
     *  @param $code  状态码
     *  @param $data  响应数据
     *  @param $message 响应消息
     */
    public function returnJson($code = 0,$message = '操作成功',$data = [])
    {
        // 登录失效
        if ($message == '1001' || $message == 'token已过期或未登录') {
            $message = 'token已过期,请重新登录';
            $code = 1001;
        }
        $returnData = [
            'code'    => $code,
            'message' => $message,
            'data'    => $data,
        ];
        return response()->json($returnData);
    }

    /**
     *  权限验证
     **/
    private function checkAccess()
    {
        $accessList = array(
            // 跳过登录验证 一律小写
            '/api/backend/users/getloginstatus',
            '/api/backend/users/getmobileagency',
            '/api/backend/users/settoken',
        );
        // 获取当前访问路由
        $path = strtolower($_SERVER['REQUEST_URI']);

        if ((in_array($path, $accessList) == false) && empty($this->token)) {
            return false;
        }
        return true;
    }

}
